using System.Collections;
using UnityEngine;

public class Enemy10 : TypeOfEnemy
{
	public void Init(int arrayNum, int weaponID)
	{
		arrayNumForAllObjectsInfo = arrayNum;
		SetEnemy();
		ChangeMotionOfEnemy(0);
		StopAllCoroutines();
		StartCoroutine(Motions());
		StartCoroutine(RefreshPosition());
		StartCoroutine(IE_Update());
		StartCoroutine(IE_DrawShadow());
		isVisibleOnScreen = false;
		int num = weaponID - 1;
		weaponID += GM.instance.appearedCountOfbossList[num] * 6;
		if (GM.instance.appearedCountOfbossList[num] < 4)
		{
			GM.instance.appearedCountOfbossList[num]++;
		}
		grabingWeaponID = weaponID;
		EquipGetOrDrop("Enemy", true, 1, grabingWeaponID);
		ChangeCharacterAnimType(GetKindOfWeaponNumber());
		SR_Helm.sprite = Sprites.instance.enemyHelmSpriteList[GetKindOfWeaponNumber() - 1];
		characterScale = ((LevelController.instance.level <= 4) ? 1f : Random.Range(1f, 1.2f));
		tf.localScale = new Vector3((!(tf.position.x > Player.instance.tf.position.x)) ? (0f - characterScale) : characterScale, characterScale, 1f);
	}

	public void ChangeMotionOfEnemy(int num)
	{
		animClipOverrides["StickMan_MotionOfEnemy0"] = AnimClips.instance.motionOfEnemy[num];
		atoc.ApplyOverrides(animClipOverrides);
	}

	private IEnumerator Motions()
	{
		Generator.instance.SpawnObj(26, tf.position, new float[0], TF_CenterBone);
		LookAtThePlayer();
		if (freezingTime <= 0f)
		{
			AppearJump();
			yield return new WaitForSeconds(0.3f);
			ShowMotionOfEnemy();
		}
		yield return new WaitForSeconds(1.5f);
		while (AllObjectsInfo.enemy[arrayNumForAllObjectsInfo].hp > 0f)
		{
			yield return new WaitForSeconds(0.1f);
			if (CheckAction())
			{
				continue;
			}
			if (freezingTime > 0f)
			{
				freezingTime -= 0.1f;
				continue;
			}
			isVisibleOnScreen = Calc.IsVisibleCharacterOnScreen(tf.position);
			LookAtThePlayer();
			if (PlayerInTheEnemyAttackZone(arrayNumForAllObjectsInfo) && Player.instance.hp > 0f)
			{
				if (SCRT_OnTheFloorSensor.onTheFloorFlag)
				{
					if (isVisibleOnScreen)
					{
						if (Random.Range(0, 3) < 2)
						{
							kindOfMotion = 1;
							Attack();
						}
						else
						{
							OtherJumpToTheTarget();
							ShowMotionOfEnemy();
						}
						continue;
					}
				}
				else if (tf.position.y > 4f)
				{
					ShowMotionOfEnemy();
					continue;
				}
			}
			if (SCRT_OnTheFloorSensor.onTheFloorFlag)
			{
				if ((Vector2.Distance(Player.instance.tf.position, AllObjectsInfo.enemy[arrayNumForAllObjectsInfo].pos) < Player.instance.hitSize + attackRange.x + 10f && Player.instance.tf.position.y > AllObjectsInfo.enemy[arrayNumForAllObjectsInfo].pos.y + 2f) || (Vector2.Distance(Player.instance.tf.position, AllObjectsInfo.enemy[arrayNumForAllObjectsInfo].pos) > Player.instance.hitSize + attackRange.x + 10f && isVisibleOnScreen))
				{
					if (Player.instance.hp > 0f)
					{
						JumpToTheTarget();
						ShowMotionOfEnemy();
					}
				}
				else if (at.GetCurrentAnimatorStateInfo(0).IsName("StickMan_Idle") && Player.instance.hp > 0f)
				{
					at.CrossFade(running_NormalList[0], 0.1f);
				}
			}
			else if (at.GetCurrentAnimatorStateInfo(0).IsName("StickMan_Idle"))
			{
				currentOnTheFloorFlag = false;
				if (!attackingNow)
				{
					at.Play(jumping_NormalList[0], -1, 0f);
				}
			}
		}
	}

	public void OtherJumpToTheTarget()
	{
		rb.velocity = ((!(tf.position.x < Player.instance.tf.position.x)) ? new Vector2(Random.Range(-3, -8), Random.Range(15, 22)) : new Vector2(Random.Range(3, 8), Random.Range(15, 22)));
	}

	public void ShowMotionOfEnemy()
	{
		attackingNow = true;
		kindOfMotion = 1;
		at.Play(motionOfEnemyList[0], -1, 0f);
		ShowAttackRange("CircleCol");
		SlowMotionControl(0.5f);
	}

	public override void Damaged(Vector2 attackedPos, AttackInfo attackInfo)
	{
		if (!(hp <= 0f))
		{
			Generator.instance.SpawnObj(9, new Vector2(TF_CenterBone.position.x, TF_CenterBone.position.y));
			hp -= attackInfo.power;
			if (LevelController.instance.level == 1)
			{
				hp -= (float)attackInfo.power * 1.5f;
			}
			AllObjectsInfo.enemy[arrayNumForAllObjectsInfo].hp = hp;
			Cam.instance.Shake(0.1f, 0.3f);
			if (attackInfo.power > 0)
			{
				GM.instance.IMG_EmenyHP.fillAmount = Calc.Percentage(hp, maxHP + maxHP * (float)LevelController.instance.level * 0.2f);
			}
			Audio.instance.PlayWeaponSFX(attackInfo.kindOfWeapon);
			ComboText.instance.AddCombo();
			GM.instance.SlowMotion(0.02f, (float)attackInfo.power * 0.0002f + 0.1f);
			if (hp <= 0f)
			{
				StartCoroutine(IE_Die(attackInfo.force));
			}
		}
	}

	private IEnumerator IE_Die(Vector2 forceVel)
	{
		COL_AttackZone.gameObject.SetActive(false);
		COL_HitBox.enabled = false;
		attackingNow = false;
		GO_Blood.SetActive(true);
		GO_Blood.transform.localScale = tf.localScale;
		yield return new WaitForSeconds(0.02f);
		at.Play("StickMan_Damaged", -1, 1f);
		EquipGetOrDrop("Enemy", false);
		AttackZoneColOn(false);
		ComboText.instance.AddScore(1000);
		Generator.instance.SpawnObj(19, tf.position);
		Audio.instance.sfx[Random.Range(30, 35)].Play();
		yield return new WaitForSeconds(0.02f);
		at.enabled = false;
		IKDynamicOn(true, forceVel);
		tf.localScale = new Vector3((!(tf.position.x > Player.instance.tf.position.x)) ? (0f - characterScale) : characterScale, characterScale, 1f);
		StartCoroutine(IE_DeActive());
	}
}
